package hczc.longcai.com.hccrowdfunding.home.fourth_mine.activities;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import hczc.longcai.com.hccrowdfunding.R;
import hczc.longcai.com.hccrowdfunding.activity.BaseActivity;

public class WithDrawActivity extends BaseActivity implements View.OnClickListener {

    private static final int STATE_HAVE_CARD = 0;
    private static final int STATE_NOT_CARD = 1;

    private int state = 1;

    private static final String TAG = "WithDrawActivity";
    private TextView zhye;
    private EditText txje;
    private TextView button;
    private RelativeLayout szyhk;
    private RelativeLayout yhk;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_with_draw);

        initViews();
    }

    private void initViews() {
        findViewById(R.id.head_back).setOnClickListener(this);
        ((TextView) findViewById(R.id.head_title)).setText("提现");

        szyhk = ((RelativeLayout) findViewById(R.id.withdraw_szyhk_layout));
        szyhk.setOnClickListener(this);
        yhk = ((RelativeLayout) findViewById(R.id.withdraw_yhk_layout));
        yhk.setOnClickListener(this);

        szyhk();

        zhye = ((TextView) findViewById(R.id.withdraw_zhye));
        txje = ((EditText) findViewById(R.id.withdraw_txje));

        button = ((TextView) findViewById(R.id.withdraw_button));
        button.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == 1){
//            Log.d(TAG,"*******************************");
            state = STATE_HAVE_CARD;
            szyhk();
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.head_back:
                finish();
                break;
            case R.id.withdraw_button:
                int visibility = szyhk.getVisibility();
                if(visibility == View.VISIBLE){
                    Toast.makeText(WithDrawActivity.this,"请先设置银行卡",Toast.LENGTH_SHORT).show();
                    break;
                }
                withdraw();
                break;
            case R.id.withdraw_szyhk_layout:
                startActivityForResult(new Intent(WithDrawActivity.this,SetCardActivity.class),1);
                break;
            case R.id.withdraw_yhk_layout:

                break;
        }
    }

    private void withdraw() {
        if (TextUtils.isEmpty(txje.getText().toString())) {
            Toast.makeText(WithDrawActivity.this, "请输入账户金额", Toast.LENGTH_SHORT).show();
            return;
        }

        if (withdrawDeopsit()) {
            Toast.makeText(WithDrawActivity.this, "提现成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(WithDrawActivity.this, "账户余额不足", Toast.LENGTH_SHORT).show();

        }
    }

    private boolean withdrawDeopsit() {
        boolean flag = false;
        String balance = zhye.getText().toString();
        String money = txje.getText().toString();
        String[] split = balance.split("元");
        for (String s : split)
//            Log.d(TAG, s);
        balance = split[0];
        Integer i1 = Integer.valueOf(balance);
        Integer i2 = Integer.valueOf(money);
        if (i1 >= i2) {
            flag = true;
        } else {
            flag = false;
        }

        return flag;
    }

    private void szyhk() {
        if(state == STATE_HAVE_CARD){
            szyhk.setVisibility(View.GONE);
            yhk.setVisibility(View.VISIBLE);
        }else if(state == STATE_NOT_CARD){
            yhk.setVisibility(View.GONE);
            szyhk.setVisibility(View.VISIBLE);
        }
    }
}
